//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.IO;
using System.Web;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for ThemeConfig.
	/// </summary>
	public class ThemeConfig
	{
       // protected static ThemedApplicationKeyApplicationConfiguration Config;

        public ThemeConfig(string name)
		{
			_name = name;
		}

   
        private string _name;
        public string Name
        {
            get{ return _name;}
        }
        
        string _parent = null;
        public string Parent
        {
            get{return _parent;}
        }

        private bool _enableCSS = true;
        public bool EnableCSS
        {
            get{ return _enableCSS;}
        }

        private bool _enableprintCSS = true;
        public bool EnablePrintCSS
        {
            get{ return _enableprintCSS;}
        }


        private string[] _roles = null;
        public string[] Roles
        {
            get{return _roles;}   
        }

		string _dateFormat = "f";
		public string DateFormat
		{
			get{ return _dateFormat;}
		}

        public bool HasRoles
        {
            get{ return Roles != null && Roles.Length > 0 && Roles[0].Trim().Length > 0; }
        }

        private string _seperator = "|";
        public string Seperator
        {
            get{ return _seperator;}
        }

        public static ThemeConfig Create(string name, ThemedApplicationKeyApplicationConfiguration config)
        {
            ThemeConfig tc = new ThemeConfig(name);

            string path = HttpContext.Current.Server.MapPath(config.ThemeLocation + name + "/skin.config");
            
            if(File.Exists(path))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
            
                XmlAttributeCollection atts = doc.SelectSingleNode("Skin").Attributes;
            
                XmlAttribute parent = atts["parent"];
                if(parent != null)
                    tc._parent = parent.Value;

                XmlAttribute roles = atts["roles"];
                if(roles != null)
                    tc._roles = roles.Value.Split(';');

				XmlAttribute date = atts["dateFormat"];
				if(date != null)
					tc._dateFormat = date.Value;

                XmlAttribute css = atts["enableCSS"];
                if(css != null)
                    tc._enableCSS = bool.Parse(css.Value);

                XmlAttribute sep = atts["seperator"];
                if(sep != null)
                    tc._seperator = sep.Value;

                XmlAttribute printcss = atts["enablePrintCSS"];
                if(printcss != null)
                    tc._enableprintCSS = bool.Parse(printcss.Value);

            }
            else
                tc._parent = config.BaseTheme;

            tc._configThemeLocation = config.ThemeLocation;
            tc._configBaseTheme = config.BaseTheme;

            return tc;
        }

        private string _configThemeLocation;
        string dirPath = null;
        public string ThemeDirectoryPath
        {
            get
            {
                if(dirPath == null)
                    dirPath = HttpContext.Current.Server.MapPath(_configThemeLocation + Name) + "";

                return dirPath;
            }
        }

        private string _configBaseTheme;
        public bool IsBase
        {
            get { return string.Compare(Name, _configBaseTheme, true) == 0; }
        }

        public bool HasParent
        {
            get
            {
                return !IsBase && !Globals.IsNullorEmpty(Parent);
            }
        }
	}
}
